package com.jiajia.study.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.R;
import com.jiajia.common.valid.SaveValid;
import com.jiajia.common.valid.UpdateValid;
import com.jiajia.study.product.dto.BrandDTO;
import com.jiajia.study.product.entity.BrandEntity;
import com.jiajia.study.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 品牌
 *
 * @author zjj
 * @email zjj@qq.com
 * @date 2025-06-09 15:37:17
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody @Validated(SaveValid.class) BrandDTO brand){

        BrandEntity brandEntity = new BrandEntity();
        BeanUtil.copyProperties(brand,brandEntity);
        brandService.save(brandEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody @Validated(UpdateValid.class) BrandDTO brand){
        BrandEntity brandEntity = new BrandEntity();
        BeanUtil.copyProperties(brand,brandEntity);
		brandService.updateInfoById(brandEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
